﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;

namespace Interface.Houses
{
    public partial class Categories : System.Web.UI.Page
    {
        DataValid dv = new DataValid();
        DataConnection dc = new DataConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["AdminSession"] == null)
            {
                Response.Redirect("~/Home.aspx");
            }
            
                bindToGrid();
            
        }

        protected DataTable dataTableInitiate()
        {
            DataTable dt = new DataTable();
            DataColumn col = dt.Columns.Add("ID", typeof(int));
            col.AutoIncrement = true;
            col.AutoIncrementStep = 1;
            col.AutoIncrementSeed = 1;
            dt.Columns.Add("STT", typeof(string));
            dt.Columns.Add("Category", typeof(string));
            
            return dt;
        }

        protected void bindToGrid()
        {

            string strsql = "Select * from Category";
            DataTable dt = dc.getData(strsql);
            DataTable dtCate = dataTableInitiate();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    int ID = Convert.ToInt32(row["IdCategory"].ToString());

                    string _name = row["Name"].ToString();

                    DataRow dtRow = dtCate.NewRow();
                    dtRow["ID"] = ID;
                    dtRow["STT"] = stt;
                    dtRow["Category"] = _name;



                    dtCate.Rows.Add(dtRow);
                }
            }


            GridCategory.DataSource = dtCate;
            GridCategory.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if(txtCategory.Text == string.Empty)
            {
                lbErrors.Text = "Bạn phải nhập vào loại nhà trọ";
            }
            else
            {
                bool checktext = dv.Isnumeric(txtCategory.Text);
                if (checktext == true)
                {
                    lbErrors.Text = "Loại Nhà Trọ Phải Là Chử";
                }
                else
                {
                    string catename = txtCategory.Text;
                    string sqlcheck = "select Name from Category where Name=N'" + catename+ "'";
                    DataTable checkname = dc.getData(sqlcheck);

                    if (checkname.Rows.Count > 0)
                    {
                        lbErrors.Text = "Loại Nhà Trọ Đã Tồn Tại";
                        return;
                    }
                    else
                    {
                        string sqladd = "insert into Category values (N'" + catename + "')";
                            try
                        {
                            dc.ExecNonQuery(sqladd);
                            lbErrors.Text = "thêm thành công";
                            txtCategory.Text = string.Empty;
                            bindToGrid();
                        }
                        catch (Exception ex)
                        {
                            lbErrors.Text = " Cập Nhật Không Thành Công";

                        }
                    }
                    
                }
            }
           }

        protected override void OnInit(EventArgs e)
        {
            GridCategory.PageIndexChanging += new GridViewPageEventHandler(GridCategory_PageIndexChanging);
            GridCategory.RowEditing += new GridViewEditEventHandler(GridCategory_RowEditing);
            GridCategory.RowUpdating += new GridViewUpdateEventHandler(GridCategory_RowUpdating);
            GridCategory.RowCancelingEdit += new GridViewCancelEditEventHandler(GridCategory_RowCancelingEdit);
            GridCategory.RowDataBound += new GridViewRowEventHandler(GridCategory_RowDataBound);

        }

        void GridCategory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataRowView drv = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    TextBox txt = (TextBox)e.Row.FindControl("txtEditName");
                    txt.Text = DataBinder.Eval(e.Row.DataItem, "Category").ToString();

                }
            }
        }

        void GridCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridCategory.EditIndex = -1;
            bindToGrid();
        }

        void GridCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
                int id = (int)GridCategory.DataKeys[e.RowIndex].Value;
                TextBox tx1 = (TextBox)GridCategory.Rows[e.RowIndex].FindControl("txtEditName");
                string cateName = tx1.Text;
                if (cateName == String.Empty)
                {
                    lbErrors.Text = "Bạn phải nhập vào tên mới";
                    return;
                }
                else
                {
                    Boolean _checked = dv.Isnumeric(cateName);
                    if (_checked == true)
                    {
                        lbErrors.Text = "Loại Nhà Trọ Không Được Là Số";
                        return;

                    }
                    else
                    {
                        string updateQuery = "update Category Set Name = N'" + cateName + "' where IdCategory = '" + id + "'";
                        dc.ExecNonQuery(updateQuery);
                        bindToGrid();
                    }
                }

                GridCategory.EditIndex = -1;
                bindToGrid();
                        
        }

        void GridCategory_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridCategory.EditIndex = e.NewEditIndex;
            bindToGrid();
        }

        void GridCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridCategory.PageIndex = e.NewPageIndex;
        }
   }
}
